function OS_Menu(host,id)
{
	this.$super = OS_Interface;
	if (this.$super(host,id)===false) return;
	delete this.$super;
	
	var selectList=[];
	var pointer=0;
	host.status.reset();
	
	var bgm=null;
	var backgroundImage="#fff";
	var imageList=[];
	
	this.load=function(handler)
	{
		for (var i=0;i<this.resourceNode.childNodes.length;i++)
		{
			switch(this.resourceNode.childNodes[i].nodeName)
			{
				case "background":
					var sourceid=this.resourceNode.childNodes[i].textContent;
					backgroundImage=OS_Resource.resource.get(sourceid);
					break;
				case "environment":
					var typeIndex=0;
					for (var j=0;j<this.resourceNode.childNodes[i].attributes.length;j++)
					{
						if (this.resourceNode.childNodes[i].attributes[j].nodeName=="type") typeIndex=j;
					}
					var playType=this.resourceNode.childNodes[i].attributes[typeIndex].nodeValue;
					var bgmid=null;
					for (var j=0;j<this.resourceNode.childNodes[i].childNodes.length;j++)
					{
						if (this.resourceNode.childNodes[i].childNodes[j].nodeName=="bgm") bgmid=this.resourceNode.childNodes[i].childNodes[j].childNodes[0].nodeValue;
					}
					bgm=OS_Resource.resource.get(bgmid);
					break;
			}
		}
		for (var i=0;i<this.resourceNode.childNodes.length;i++)
		{
			switch(this.resourceNode.childNodes[i].nodeName)
			{
				case "imageList":
					var images=this.resourceNode.childNodes[i].childNodes;
					for (var j=0;j<images.length;j++)
					if (images[j].nodeType==1 && images[j].nodeName=="imageArea")
					{
						var imageTemp={};
						var image=images[j];
						var source=image.getElementsByTagName("source")[0].childNodes[0].nodeValue;
						imageTemp.imageres=OS_Resource.resource.get(source);
						var position=image.getElementsByTagName("position")[0];
						imageTemp.position={
									x:position.getElementsByTagName("x")[0].childNodes[0].nodeValue,
									y:position.getElementsByTagName("y")[0].childNodes[0].nodeValue,
						};
						imageList.push(imageTemp);
					}
					break;
			}
		}
		for (var i=0;i<this.resourceNode.childNodes.length;i++)
		{
			switch(this.resourceNode.childNodes[i].nodeName)
			{
				case "selectList":
					var selects=this.resourceNode.childNodes[i].childNodes;
					for (var j=0;j<selects.length;j++)
					if (selects[j].nodeType==1 && selects[j].nodeName=="select")
					{
						var selectTemp={};
						var select=selects[j];
						var index=select.getAttribute("index");
						var text=select.getElementsByTagName("text")[0];
						var position=select.getElementsByTagName("position")[0];
						selectTemp.text={
							content:text.childNodes[0].nodeValue,
							size:text.getAttribute("size"),
							align:text.getAttribute("align"),
							shadow:text.getAttribute("shadow"),
							color:text.getAttribute("color"),
							bold:text.getAttribute("bold")
						};
						selectTemp.position={
									x:position.getElementsByTagName("x")[0].childNodes[0].nodeValue,
									y:position.getElementsByTagName("y")[0].childNodes[0].nodeValue,
						};
						selectTemp.act=function(){};
						if (select.getElementsByTagName("goto").length>0)
						{
							var interfaceid=select.getElementsByTagName("goto")[0].childNodes[0].nodeValue;
							var interfacetype="scene";
							if (select.getElementsByTagName("goto")[0].hasAttribute("type"))
							{
								interfacetype=select.getElementsByTagName("goto")[0].getAttribute("type");
							}
							var changemusic=true;
							if (select.getElementsByTagName("goto")[0].hasAttribute("cbgm"))
							{
								changemusic=eval(select.getElementsByTagName("goto")[0].getAttribute("cbgm"));
							}
							(function(id,type,cbgm){
								selectTemp.act=function(){
									host.changeInterface(id,type,cbgm);
								};
							})(interfaceid,interfacetype,changemusic);
						}
						selectList[index]=selectTemp;
					}
					break;
			}
		}
		this.loaded=true;
		handler(this);
	}
	
	var pointerUp=function()
	{
		pointer--;
		if (pointer<0)
		{
			pointer=selectList.length-1;
		}
	}
	
	var pointerDown=function()
	{
		pointer++;
		if (pointer>=selectList.length)
		{
			pointer=0;
		}
	}
	
	var update=function()
	{
		var upkey=host.status.checkKeyPressed("up");
		var downkey=host.status.checkKeyPressed("down");
		var enterkey=host.status.checkKeyPressed("enter");
		if (enterkey)
		{
			selectList[pointer].act();
		}
		else if (upkey)
		{
			pointerUp();
		}
		else if (downkey)
		{
			pointerDown();
		}
	}
	
	this.refresh=function()
	{
		host.stage.clear(backgroundImage);
		update();
		for (var i=0;i<imageList.length;i++)
		{
			var image=imageList[i];
			var left=OS_Common.eval(image.position.x);
			var top=OS_Common.eval(image.position.y);
			host.stage.getContext().drawImage(image.imageres.getImage(),left,top);
		}
		for (var i=0;i<selectList.length;i++)
		{
			var select=selectList[i];
			var text={};
			text.content=select.text.content;
			text.size=select.text.size;
			text.align=select.text.align;
			text.color="#000";
			if (!!select.text.color){text.color=select.text.color;};
			if (select.text.bold=="true"){text.bold=true;}
			if (select.text.shadow=="true")
			{
				text.shadow={};
				text.shadow.offsetX=0;
				text.shadow.offsetY=0;
				text.shadow.blur=10;
				text.shadow.color="#000";
			}
			if (pointer==i)
			{
				var duration=1000;
				var nowTime=(new Date()).getTime();
				var alpha=Math.sin(Math.PI*(nowTime%duration/duration))*0.2+0.2;
				text.background="rgba(255,255,255,"+alpha+")";
			}
			var left=OS_Common.eval(select.position.x);
			var top=OS_Common.eval(select.position.y);
			host.stage.write(text,left,top);
		}
	}
	
	this.playBGM=function()
	{
		if (!!bgm)
		{
			host.studio.playBGM([bgm],false);
		}
	}
}

OS_Resource.Builder.put("menu",OS_Menu);